package com.dianxin.smartpark.service.order.impl;

import java.util.List;

import com.chinatelecom.response.common.CommonResponseBody;
import com.chinatelecom.response.utils.BeanUtil;
import com.chinatelecom.response.utils.ResponseBodyWrapperUtil;
import com.chinatelecom.response.constant.ErrMsgEnum;

import com.chinatelecom.response.utils.StringUtils;
import com.dianxin.smartpark.dao.dataobject.order.SmartParkOrderRecord;
import com.dianxin.smartpark.dao.dataobject.order.SmartParkOrderRecordExample;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkProperty;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyCleaning;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyRepair;
import com.dianxin.smartpark.dao.dataobject.property.SmartParkPropertyWater;
import com.dianxin.smartpark.dao.mapper.attendance.SmartParkAttendanceRecordMapper;
import com.dianxin.smartpark.dao.mapper.order.SmartParkOrderRecordMapper;
import com.dianxin.smartpark.dao.mapper.property.SmartParkPropertyCleaningMapper;
import com.dianxin.smartpark.dao.mapper.property.SmartParkPropertyRepairMapper;
import com.dianxin.smartpark.dao.mapper.property.SmartParkPropertyWaterMapper;
import com.dianxin.smartpark.dao.qo.order.SmartParkOrderRecordQO;
import com.dianxin.smartpark.dao.qo.property.SmartParkPropertyQO;
import com.dianxin.smartpark.manager.common.ISmartParkMessageService;
import com.dianxin.smartpark.manager.order.ISmartParkOrderRecordService;
import com.dianxin.smartpark.service.order.SmartParkOrderRecordService;
import com.dianxin.smartpark.service.vo.order.SmartParkOrderHandleVO;
import com.dianxin.smartpark.service.vo.order.SmartParkOrderRecordVO;
import com.dianxin.smartpark.service.vo.property.SmartParkPropertyVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 工单处理记录业务层处理
 * 
 * @author chenjiasong
 * @date 2021-07-22
 */
@Service
public class SmartParkOrderRecordServiceImpl implements SmartParkOrderRecordService
{
    private final static Logger logger = LoggerFactory.getLogger(SmartParkOrderRecordServiceImpl.class);
    @Resource
    private SmartParkOrderRecordMapper smartParkOrderRecordMapper;

    @Resource
    private SmartParkPropertyCleaningMapper smartParkPropertyCleaningMapper;

    @Resource
    private SmartParkPropertyRepairMapper smartParkPropertyRepairMapper;

    @Resource
    private SmartParkPropertyWaterMapper smartParkPropertyWaterMapper;

    @Resource
    private ISmartParkOrderRecordService iSmartParkOrderRecordService;

    @Resource
    private SmartParkAttendanceRecordMapper smartParkAttendanceRecordMapper;

    @Resource
    private ISmartParkMessageService iSmartParkMessageService;

    /**
     * 查询工单处理记录
     * 
     * @param id 工单处理记录ID
     * @return 工单处理记录
     */
    @Override
    public CommonResponseBody<SmartParkOrderRecordVO> selectSmartParkOrderRecordById(Long id)
    {
        SmartParkOrderRecord smartParkOrderRecord = smartParkOrderRecordMapper.selectByPrimaryKey(id);
        return ResponseBodyWrapperUtil.successWrapper(smartParkOrderRecord,SmartParkOrderRecordVO.class);
    }

    /**
     * 查询工单处理记录列表
     * 
     * @param qo 工单处理记录
     * @return 工单处理记录
     */
    @Override
    public CommonResponseBody<List<SmartParkOrderRecordVO>> selectSmartParkOrderRecordList(SmartParkOrderRecordQO qo)
    {
        SmartParkOrderRecordExample smartParkOrderRecordExample = new SmartParkOrderRecordExample();
        SmartParkOrderRecordExample.Criteria criteria = smartParkOrderRecordExample.createCriteria();
        if (qo.getTenantId() != null) {
            criteria.andTenantIdEqualTo(qo.getTenantId());
        }
        if (qo.getBusiId() != null) {
            criteria.andBusiIdEqualTo(qo.getBusiId());
        }
        if (qo.getBusiType() != null) {
            criteria.andBusiTypeEqualTo(qo.getBusiType());
        }

        criteria.andIsDeletedEqualTo(0);
        if (qo.getNeedPage()) {
            smartParkOrderRecordExample.setOrderByClause("gmt_create asc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        } else {
            smartParkOrderRecordExample.setOrderByClause("gmt_create asc");
        }
        List<SmartParkOrderRecord> passRecords = smartParkOrderRecordMapper.selectByExample(smartParkOrderRecordExample);
        long count = smartParkOrderRecordMapper.countByExample(smartParkOrderRecordExample);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkOrderRecordVO.class);
    }


    /**
     * 查询工单处理记录列表
     *
     * @param qo 工单处理记录
     * @return 工单处理记录
     */
    @Override
    public CommonResponseBody<List<SmartParkPropertyVO>> selectSmartParkPropertyList(SmartParkPropertyQO qo)
    {
        // 验证 todo
        SmartParkProperty smartParkProperty = new SmartParkProperty();
        try {
            BeanUtil.insertConversion(qo, smartParkProperty);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkProperty.setOrderByPage("order by t1.gmt_create desc limit " + (qo.getPageIndex() - 1) * qo.getPageSize() + "," + qo.getPageSize());
        List<SmartParkProperty> passRecords = smartParkOrderRecordMapper.selectSmartParkPropertyList(smartParkProperty);
        long count = smartParkOrderRecordMapper.countBytSmartParkProperty(smartParkProperty);
        return ResponseBodyWrapperUtil
                .successListWrapper(passRecords, count, qo, SmartParkPropertyVO.class);
    }

    /**
     * 新增工单处理记录
     * 
     * @param vo 工单处理记录
     * @return 结果
     */
    @Override
    public CommonResponseBody<Boolean> insertSmartParkOrderRecord(SmartParkOrderRecordVO vo)
    {
        // 验证 todo
        SmartParkOrderRecord smartParkOrderRecord = new SmartParkOrderRecord();
        try {
            BeanUtil.insertConversion(vo, smartParkOrderRecord);
        } catch (Exception e) {
            logger.error("", e);
        }
        smartParkOrderRecord.beforeInsert();
        return ResponseBodyWrapperUtil.toResData(smartParkOrderRecordMapper.insertSelective(smartParkOrderRecord));
    }


    /**
     * 工单处理
     *
     * @param smartParkOrderHandleVO 工单处理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public CommonResponseBody<Boolean> handleOrder(SmartParkOrderHandleVO smartParkOrderHandleVO)
    {
        String msgContent = "";
        String handleText = "你提交的{$$}工单已{%%}";
        //1 受理  2 完成  3撤回  4转交
        if(smartParkOrderHandleVO.getHandleType() == 1){
            handleText = "受理";
        }else if(smartParkOrderHandleVO.getHandleType() == 2){
            handleText = "执行";
        }else if(smartParkOrderHandleVO.getHandleType() == 3){
            handleText = "撤回";
        }else if(smartParkOrderHandleVO.getHandleType() == 4){
            handleText = "转交";
        }
        int res = 0;
        String msgTitle = "工单：";
        //修改主表状态 1 保洁  2 维修  3送水
        if(smartParkOrderHandleVO.getBusiType() == 1){
            msgTitle+="保洁";
            handleText = "你提交的保洁工单已{%%}".replace("{%%}",handleText);
            SmartParkPropertyCleaning smartParkPropertyCleaning = smartParkPropertyCleaningMapper.selectByPrimaryKey(smartParkOrderHandleVO.getBusiId());
            if(StringUtils.isNull(smartParkPropertyCleaning)){
               return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
            }

            if(smartParkOrderHandleVO.getHandleType() == 1 && smartParkPropertyCleaning.getStatus()!= 0){
                return ResponseBodyWrapperUtil.fail("任务已受理","400");
            }

            if(smartParkOrderHandleVO.getHandleType() == 2 && smartParkPropertyCleaning.getStatus()!= 1){
                return ResponseBodyWrapperUtil.fail("任务已完成","400");
            }
            smartParkPropertyCleaning.setId(smartParkOrderHandleVO.getBusiId());
            smartParkPropertyCleaning.setStatus(smartParkOrderHandleVO.getHandleType());
            smartParkPropertyCleaning.setHandlerUser(smartParkOrderHandleVO.getHandlerUser());
            res = smartParkPropertyCleaningMapper.updateByPrimaryKeySelective(smartParkPropertyCleaning);
        }else if(smartParkOrderHandleVO.getBusiType() == 2){
            msgTitle+="报修";
            handleText = "你提交的报修工单已{%%}".replace("{%%}",handleText);
            SmartParkPropertyRepair smartParkPropertyRepair = smartParkPropertyRepairMapper.selectByPrimaryKey(smartParkOrderHandleVO.getBusiId());
            if(StringUtils.isNull(smartParkPropertyRepair)){
                return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
            }

            if(smartParkOrderHandleVO.getHandleType() == 1 && smartParkPropertyRepair.getStatus()!= 0){
                return ResponseBodyWrapperUtil.fail("任务已受理","400");
            }

            if(smartParkOrderHandleVO.getHandleType() == 2 && smartParkPropertyRepair.getStatus()!= 1){
                return ResponseBodyWrapperUtil.fail("任务已完成","400");
            }
            smartParkPropertyRepair.setStatus(smartParkOrderHandleVO.getHandleType());
            smartParkPropertyRepair.setId(smartParkOrderHandleVO.getBusiId());
            smartParkPropertyRepair.setHandlerUser(smartParkOrderHandleVO.getHandlerUser());
            res = smartParkPropertyRepairMapper.updateByPrimaryKeySelective(smartParkPropertyRepair);
        }else if(smartParkOrderHandleVO.getBusiType() == 3){
            msgTitle+="送水";
            handleText = "你提交的送水工单已{%%}".replace("{%%}",handleText);
            SmartParkPropertyWater smartParkPropertyWater = smartParkPropertyWaterMapper.selectByPrimaryKey(smartParkOrderHandleVO.getBusiId());
            if(StringUtils.isNull(smartParkPropertyWater)){
                return ResponseBodyWrapperUtil.fail(ErrMsgEnum.ERROR_PARAME);
            }

            if(smartParkOrderHandleVO.getHandleType() == 1 && smartParkPropertyWater.getStatus()!= 0){
                return ResponseBodyWrapperUtil.fail("任务已受理","400");
            }

            if(smartParkOrderHandleVO.getHandleType() == 2 && smartParkPropertyWater.getStatus()!= 1){
                return ResponseBodyWrapperUtil.fail("任务已完成","400");
            }
            smartParkPropertyWater.setStatus(smartParkOrderHandleVO.getHandleType());
            smartParkPropertyWater.setId(smartParkOrderHandleVO.getBusiId());
            smartParkPropertyWater.setHandlerUser(smartParkOrderHandleVO.getHandlerUser());
            res = smartParkPropertyWaterMapper.updateByPrimaryKeySelective(smartParkPropertyWater);
        }
        //增加工单记录
        if(res > 0){
            res = iSmartParkOrderRecordService.insertSmartParkOrderRecord(smartParkOrderHandleVO.getTenantId(),smartParkOrderHandleVO.getHandleType(),smartParkOrderHandleVO.getHandlerUser(),smartParkOrderHandleVO.getBusiId(),smartParkOrderHandleVO.getBusiType(),smartParkOrderHandleVO.getHandleType(),handleText,smartParkOrderHandleVO.getRemark());
            msgContent = "您有一条"+msgTitle+"工单需要处理。";
            iSmartParkMessageService.insertSmartParkMessage(smartParkOrderHandleVO.getBusiId(),smartParkOrderHandleVO.getBusiType(),msgTitle,msgContent,smartParkOrderHandleVO.getTenantId(),smartParkOrderHandleVO.getHandlerUser());
        }
        return ResponseBodyWrapperUtil.toResData(res);
    }


    /**
     * 删除工单处理记录信息
     * 
     * @param id 工单处理记录ID
     * @return 结果08
     */
    @Override
    public CommonResponseBody<Boolean> deleteSmartParkOrderRecordById(Long id)
    {
        SmartParkOrderRecord smartParkOrderRecord = new SmartParkOrderRecord();
        smartParkOrderRecord.beforeDelete();
        smartParkOrderRecord.setId(id);
        return ResponseBodyWrapperUtil.toResData(smartParkOrderRecordMapper.updateByPrimaryKeySelective(smartParkOrderRecord));
    }
}
